package com.wwdx.service.impl;

import com.wwdx.wechat.config.BaseWxConfig;
import com.wwdx.wechat.handler.*;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;

import javax.annotation.PostConstruct;

/**
 * @author liguoliang
 */
public abstract class BaseWxService extends WxMpServiceImpl {

    private WxMpMessageRouter router;

    @PostConstruct
    public void init(){
        final WxMpInMemoryConfigStorage config = new WxMpInMemoryConfigStorage();
        // 设置微信公众号的appid
        config.setAppId(this.getServerConfig().getAppid());
        // 设置微信公众号的app corpSecret
        config.setSecret(this.getServerConfig().getAppsecret());
        // 设置微信公众号的token
        config.setToken(this.getServerConfig().getToken());
        // 设置消息加解密密钥
        config.setAesKey(this.getServerConfig().getAesKey());
        super.setWxMpConfigStorage(config);
        this.refreshRouter();
    }

    /**
     * 微信公众号配置类
     * @return BaseWxConfig 子类
     */
    protected abstract BaseWxConfig getServerConfig();

    /**
     * 微信菜单处理类
     * @return AbstractMenuHandler 子类
     */
    protected abstract AbstractMenuHandler getMenuHandler();

    /**
     * 微信关注处理类
     * @return AbstractSubscribeHandler 子类
     */
    protected abstract AbstractSubscribeHandler getSubscribeHandler();

    /**
     * 微信取关处理类
     * @return AbstractUnsubscribeHandler 子类
     */
    protected abstract AbstractUnsubscribeHandler getUnsubscribeHandler();

    /**
     * 微信消息处理类
     * @return AbstractMsgHandler 子类
     */
    protected abstract AbstractMsgHandler getMsgHandler();

    /**
     * 微信二维码扫描处理类
     * @return AbstractScanHandler 子类
     */
    protected abstract AbstractScanHandler getScanHandler();

    private void refreshRouter() {

        final WxMpMessageRouter newRouter = new WxMpMessageRouter(this);

        //点击菜单点击事件推送
        newRouter.rule().msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.MenuButtonType.CLICK).handler(this.getMenuHandler()).end();

        //关注事件
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.EventType.SUBSCRIBE).handler(this.getSubscribeHandler()).end();

        //取消关注事件
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.EventType.UNSUBSCRIBE).handler(this.getUnsubscribeHandler()).end();

        //扫码事件
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.EventType.SCAN).handler(this.getScanHandler()).end();

        //默认消息事件
        newRouter.rule().async(false).handler(this.getMsgHandler()).end();

        this.router = newRouter;
    }

    /**
     * 消息路由
     * @param message 消息实体
     * @return WxMpXmlOutMessage
     */
    public WxMpXmlOutMessage route(WxMpXmlMessage message) {
        return this.router.route(message);
    }
}